﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils.Drawing
{
    public class Animation
    {
        int firstFrame;
        int lastFrame;
        float animationSpeed; // Frames per second
        AnimationStrategy animationStrategy;

        public int FirstFrame { get {return firstFrame;} }
        public int LastFrame { get {return lastFrame;} }

        public delegate bool AnimationStrategy(ref float currentFrame, float updateBy, int firstFrame, int lastFrame);

        public static bool AnimationStrategySingle(ref float currentFrame, float updateBy, int firstFrame, int lastFrame)
        {
            currentFrame += updateBy;
            if ((int)currentFrame > lastFrame)
            {
                currentFrame = lastFrame;
                return false;
            }
            return true;
        }

        public static bool AnimationStrategySingleEndless(ref float currentFrame, float updateBy, int firstFrame, int lastFrame)
        {
            currentFrame += updateBy;
            if ((int)currentFrame > lastFrame)
            {
                currentFrame = lastFrame;
            }
            return true;
        }

        public static bool AnimationStrategyLoop(ref float currentFrame, float updateBy, int firstFrame, int lastFrame)
        {
            currentFrame += updateBy;
            if ((int)currentFrame > lastFrame)
            {
                currentFrame = firstFrame;
            }
            return true;
        }

        /*
         * animationSpeed = Frames/second
         */
        public Animation(int firstFrame, int lastFrame, float animationSpeed, AnimationStrategy animationStrategy)
        {
            this.firstFrame = firstFrame;
            this.lastFrame = lastFrame;
            this.animationSpeed = animationSpeed;
            this.animationStrategy = animationStrategy;
        }

        public bool Update(ref float currentFrame, float dTime)
        {
            return animationStrategy(ref currentFrame, dTime * animationSpeed, firstFrame, lastFrame);
        }
    }
}
